package com.example.Bean;

import java.util.List;
import java.util.Random;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.example.Table.Question;
import com.example.util.HibernateUtil;

@ManagedBean(name="questionBean", eager = true)
@SessionScoped
public class QuestionBean {
	private int id;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	private String question;
	private String variant_a;
	private String variant_b;
	private String variant_c;
	private String variant_d;
	private String answer;
	public String getQuestion() {
		return question;
	}
	public void setQuestion(String question) {
		this.question = question;
	}
	public String getVariant_a() {
		return variant_a;
	}
	public void setVariant_a(String variant_a) {
		this.variant_a = variant_a;
	}
	public String getVariant_b() {
		return variant_b;
	}
	public void setVariant_b(String variant_b) {
		this.variant_b = variant_b;
	}
	public String getVariant_c() {
		return variant_c;
	}
	public void setVariant_c(String variant_c) {
		this.variant_c = variant_c;
	}
	public String getVariant_d() {
		return variant_d;
	}
	public void setVariant_d(String variant_d) {
		this.variant_d = variant_d;
	}
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}

	@SuppressWarnings({ "static-access", "unchecked"})
	public String forward(){
		Session session=new HibernateUtil().getSessionFactory().openSession();
		Transaction tx=session.beginTransaction();
		List<Question> questionDB=(List<Question>) session.createQuery("from Question where viewed = 0").list();
		Random r=new Random();
		int a=r.nextInt(questionDB.size());
		this.id=questionDB.get(a).getId();
		this.question=questionDB.get(a).getQuestion();
		this.answer=questionDB.get(a).getAnswer();
		this.variant_a=questionDB.get(a).getVariant_a();
		this.variant_b=questionDB.get(a).getVariant_b();
		this.variant_c=questionDB.get(a).getVariant_c();
		this.variant_d=questionDB.get(a).getVariant_d();
		tx.commit();
		session.close();
		return "Question";
	}
}
